package com.weed.table.head;


import java.util.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
import com.weed.table.renderer.ColorRenderer;

/*** 函数说明：
 *     扩展列头显示。
 * 作者     日期     说明
 * 卓志成   20080119  整理JTable.
 *
 */

public class ColumnGroup {

  protected TableCellRenderer renderer;
  protected Vector v;
  protected String text;
  protected int margin = 0;
  /**
   * 列组的名称.
   */
  public ColumnGroup(String text) {
    this(null, text);
  }

  /**
   * 通过表的汇制者.
   * @param renderer TableCellRenderer
   * @param text ColumnGroup Text.
   */

  public ColumnGroup(TableCellRenderer renderer, String text) {
    if (renderer == null) {
      this.renderer = new ColorRenderer() {
        public Component getTableCellRendererComponent(JTable table,
            Object value,
            boolean isSelected, boolean hasFocus, int row, int column) {
          JTableHeader header = table.getTableHeader();
          if (header != null) {
            setForeground(header.getForeground());
            setBackground(header.getBackground());
            setFont(header.getFont());
          }
          setHorizontalAlignment(JLabel.CENTER);
          setText( (value == null) ? "" : value.toString());
          setBorder(UIManager.getBorder("TableHeader.cellBorder"));
          return this;
        }
      };
    }
    else {
      this.renderer = renderer;
    }
    this.text = text;
    v = new Vector();
  }

  /**
   * @param obj    TableColumn or ColumnGroup
   */
  public void add(Object obj) {
    if (obj == null) {
      return;
    }
    v.addElement(obj);
  }

  /**
   * @param c    TableColumn
   * @param v    ColumnGroups
   */
  public Vector getColumnGroups(TableColumn c, Vector g) {
    g.addElement(this);
    if (v.contains(c))
      return g;
    Enumeration enum1 = v.elements();
    while (enum1.hasMoreElements()) {
      Object obj = enum1.nextElement();
      if (obj instanceof ColumnGroup) {
        Vector groups =
            (Vector) ( (ColumnGroup) obj).getColumnGroups(c, (Vector) g.clone());
        if (groups != null)
          return groups;
      }
    }
    return null;
  }

  /**
   * 返回TableCellRenderer,Default :DefaultTableCellRenderer
   */

  public TableCellRenderer getHeaderRenderer() {
    return renderer;
  }

  /**
   * 设置 TableCellRenderer; when TableCellRenderer is not null;
   */
  public void setHeaderRenderer(TableCellRenderer renderer) {
    if (renderer != null) {
      this.renderer = renderer;
    }
  }

  /**
   * get Header Value;
   * @return
   */
  public Object getHeaderValue() {
    return text;
  }

  /**
   * 获得Table中TableCellRendererComponent的位置�?
   */

  public Dimension getSize(JTable table) {
    Component comp = renderer.getTableCellRendererComponent(
        table, getHeaderValue(), false, false, -1, -1);
    int height = comp.getPreferredSize().height;
    int width = 0;
    Enumeration enum1 = v.elements();
    while (enum1.hasMoreElements()) {
      Object obj = enum1.nextElement();
      if (obj instanceof TableColumn) {
        TableColumn aColumn = (TableColumn) obj;
        width += aColumn.getWidth();
      }
      else {
        width += ( (ColumnGroup) obj).getSize(table).width;
      }
    }
    return new Dimension(width, height);
  }

  /**
   * 设置ColumnGroup的位置。
   */

  public void setColumnMargin(int margin) {
    this.margin = margin;
    Enumeration enum1 = v.elements();
    while (enum1.hasMoreElements()) {
      Object obj = enum1.nextElement();
      if (obj instanceof ColumnGroup) {
        ( (ColumnGroup) obj).setColumnMargin(margin);
      }
    }
  }
}
